<?php

namespace App\Listeners;

use App\Events\BlogView;
use App\Models\Post;
use Carbon\Carbon;
use Illuminate\Events\Dispatcher;

class SomeSubscriber
{
    /**
     * Handle user login events.
     */
    public function onUserLogin($event)
    {
//        Auth::user()->last_login = new DateTime;
//        Auth::user()->last_login_ip = Request::getClientIp();
//        Auth::user()->save();
        $event->user->last_login_at = Carbon::now();
        $event->user->last_login_ip = \Request::getClientIp();
        $event->user->save();
    }

    public function onBlogView($event)
    {
        /**
         * @var  $post Post
         */
        $post = $event->post;
        if ($post) {
            $post->view_count = (int)$post->view_count + 1;
            $post->save();
        }
    }

    /**
     * Handle user logout events.
     */
    public function onUserLogout($event)
    {

    }

    /**
     * Register the listeners for the subscriber.
     *
     * @param  Dispatcher $events
     */
    public function subscribe($events)
    {
//        $events->listen(
//            'Illuminate\Auth\Events\Login',
//            'App\Listeners\SomeSubscriber@onUserLogin'
//        );
//
//        $events->listen(
//            'Illuminate\Auth\Events\Logout',
//            'App\Listeners\SomeSubscriber@onUserLogout'
//        );
        $events->listen(
            BlogView::class,
            SomeSubscriber::class."@onBlogView"
//            'App\Listeners\SomeSubscriber@onBlogView'
        );
    }

}